/*
LR909 
@totetmatt
*/
#include "ColorState.h"
#include "Transition.h"
#include "SerialManager.h"
#include "EdiflobMessage.h"
#include "Arduino.h"
int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 10;    // how many points to fade the LED by

int static const RED_PIN = 6;
int static const GREEN_PIN = 5;
int static const BLUE_PIN = 3;

 
ColorState* _colorSequence[] = {
new ColorState(0,0,128,true,5000),

new ColorState(0,64,0,true,2000),
new ColorState(64,0,0,true,500)
};
SerialManager* serial;
Transition* t;
ColorState* _currentState;
int _currentStateIndex;
void setup()  { 

  
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  _currentStateIndex=1;
  _currentState = _colorSequence[_currentStateIndex];
  
  t = new Transition(_colorSequence[0],_colorSequence[1]);
  
  serial  =new SerialManager();
  
} 

// --- Déclaration des variables globales ---
String content = "";
void loop()  { 
 
 serial->checkRoutine();
 if(serial->haveMessage())
 {
    content=serial->getMessage();
    EdiflobMessage test = EdiflobMessage::Deserialize(content);
    serial->sendMessage(test.getCommand());
    serial->sendMessage(test.getRed());
 }     
  /* */
  
  outputColor(t->currentState());

  t->doStep();

  t->doTransition();

  if(t->isFinish())
  {
    _currentStateIndex++;
    if(_currentStateIndex >= 3)
    {
      _currentStateIndex = 0;
    }
    t->nextState(_colorSequence[_currentStateIndex]);
  }
  
  
}

void outputColor(ColorState* c)
{
   outputColor(c->getRed(),c->getGreen(),c->getBlue());
}

void outputColor(int r,int g,int b)
{
  analogWrite(RED_PIN, r);    
  analogWrite(GREEN_PIN, g);    
  analogWrite(BLUE_PIN, b);    
}

